/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using System.IO;
using DaveSexton.DocProject.Wizards.Foundation;
using DaveSexton.DocProject.Wizards;
using System.Text.RegularExpressions;
using System.Xml.XPath;
using System.Xml;
using DaveSexton.DocProject.Sandcastle.Configuration;
using DaveSexton.DocProject.Sandcastle.ContentManagement;

namespace DaveSexton.DocProject.Sandcastle
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class CreateSharedContentWizardStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return Resources.WizardText.CreateSharedContentName;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}

		public override bool SuppressDefaultNavigation
		{
			get
			{
				// this allows the user to press enter in the editor without 
				// it automatically clicking the default button on the dialog
				return true;
			}
		}
		#endregion

		#region Private / Protected
		private CreateSharedContentControl control;
		private XmlContentManager manager;
		private string lastPresentationPath;
		private IDocProject lastImportFromProject;
		private readonly ChoosePresentationWizardStep presentationStep;
		private readonly IDocProject project;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="CreateSharedContentWizardStep" /> class.
		/// </summary>
		public CreateSharedContentWizardStep(IDocProject project, ChoosePresentationWizardStep presentationStep)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			if (presentationStep == null)
				throw new ArgumentNullException("presentationStep");

			this.project = project;
			this.presentationStep = presentationStep;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			if (preloading)
				// this step cannot be preloaded because a presentation must be selected
				return null;

			Presentation presentation = presentationStep.Presentation;

			if (lastPresentationPath == null || !lastPresentationPath.Equals(presentation.Path) 
				|| lastImportFromProject != presentation.ImportFrom)
			{
				lastPresentationPath = presentation.Path;
				lastImportFromProject = presentation.ImportFrom;

				if (lastImportFromProject == null || !PresentationImporter.ShouldImportPresentationContent(project, presentation))
				{
					string fullPresentationPath = Path.Combine(SandcastleBuildEngineProvider.Paths.Presentation, lastPresentationPath);

					manager = new XmlContentManager(project, presentation, fullPresentationPath, true);
				}
				else
				{
					manager = new XmlContentManager(project, presentation, lastImportFromProject);
				}

				// Load last committed values or saved content if none has been committed yet.
				manager.Load();

				control = new CreateSharedContentControl(manager);
				control.Editor.ItemChanged += delegate(object sender, ContentItemChangedEventArgs e)
				{
					OnStateChanged(new WizardStepStateChangedEventArgs(((sender) as XmlContentDocumentsEditor).IsSelectedItemValidXml, true));
				};
			}

			return control;
		}

		protected override bool CanNavigateAway(WizardController controller, WizardStep toStep, bool forward)
		{
			return !forward || (control != null && control.Editor.IsSelectedItemValidXml);
		}

		protected override void WizardFinished(WizardController controller)
		{
			Presentation presentation = presentationStep.Presentation;

			string root = SandcastleBuildEngineProvider.ProjectPaths.RelativeHelpRoot;

			try
			{
				if (control != null)
				{
					IProjectItem media = project.GetFolder(Path.Combine(root, presentation.MediaDirectory));

					// create the media directory if it doesn't exist
					media.Show();

					if (manager.ImportAllResources() > 0)
						media.Refresh();

					manager.CommitChanges();
				}
			}
			finally
			{
				manager.SaveChanges(Path.Combine(Path.Combine(Path.Combine(
					project.Directory, 
					root),
					SandcastleBuildEngineProvider.ProjectPaths.Presentation), 
					presentation.ContentDirectory));
			}
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// do nothing
		}
		#endregion
	}
}
